﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Newtonsoft.Json;

namespace cadbom
{
    public partial class PdfProgressForm : Form
    {
        private Action<int> callback;
        OutMsg outMsg = new OutMsg();
        private String productCode;
        public PdfProgressForm()
        {
            InitializeComponent();
        }
        public PdfProgressForm(String productCode ,Action<int> callback)
        {
            InitializeComponent();
            this.callback = callback;
            this.productCode = productCode;            
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }
        private void UpdateProgressBar()
        {
            for (int i = 0; i <= 100; i++)
            {
                // 跨线程操作进度条，需要使用Invoke
                this.Invoke(new Action(() =>
                {
                    progressBar1.Value = i;
                }));
                //Thread.Sleep(100); // 模拟任务的执行时间
                Task.Delay(TimeSpan.FromSeconds(2)).Wait(); //等待2秒
                String OutPath = GValues.pdfPath + "\\selectDWGWindow.pdf";
                // 判断文件是否存在
                if (File.Exists(OutPath))
                {
                    
                    break;

                }
            }
            //callback(0);
        }

        public void beginProcess()
        {
            // 设置进度条的最小值和最大值
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Value = 0;

            // 模拟一个长任务
            for (int i = 0; i <= 100; i++)
            {
                // 设置进度条的当前值
                progressBar1.Value = i;

                Task.Delay(TimeSpan.FromMilliseconds(300)).Wait(); //等待200毫秒秒
                String OutPath = GValues.pdfPath + "\\selectDWGWindow.pdf";
                // 判断文件是否存在
                if (FileExistenceChecker.CheckFileExists(OutPath))
                {
                    progressBar1.Value = 100;

                    break;

                }
            }
            this.button1.Visible = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            uploadPdf();
            this.Close();
        }
        public void uploadPdf()
        {
            try
            {
                Http.CreateHttpClient(GValues.domainLink + "sys/common/upload?biz=" + "drawing/" + GValues.tenant + "/plot", GValues.tenant, GValues.token).UploadFileAsync(GValues.pdfPath + "\\selectDWGWindow.pdf", new Action<Http.HttpResp>((r1) =>
                {
                    var responCode = Convert.ToInt32(r1.Code);
                    if (responCode == 200) //访问成功
                    {
                        ResponResult<String> cc = JsonConvert.DeserializeObject<ResponResult<String>>(r1.AckStr());
                        BasProduct basProduct = new BasProduct();
                        basProduct.attachment = cc.message;
                        basProduct.code = this.productCode;;
                        saveProductAttachemt(basProduct);
                    }
                    else
                    {
                        this.outMsg.AddMsg("上传图纸报错！" + r1.Code + " " + " " + responCode + " " + r1.Error);
                    }
                }));
            }
            catch (System.Exception e)
            {
                this.outMsg.AddMsg("上传图纸报错！" + e.Message);
            }
        }

        public void saveProductAttachemt(BasProduct basProduct)
        {
            try
            {
                string postData = JsonConvert.SerializeObject(basProduct);
                Http.CreateHttpClient(GValues.domainLink + "bas/basProduct/cad-edit", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
                {
                    var responCode = Convert.ToInt32(r.Code);
                    if (responCode == 200) //访问成功
                    {
                        this.outMsg.AddMsg("上传图纸成功！");
                    }
                    else
                    {
                        this.outMsg.AddMsg("上传附件数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);
                    }
                }));
            }
            catch (System.Exception e)
            {
                this.outMsg.AddMsg("上传附件报错！" + e.Message);
            }
        }
    }
    public class FileExistenceChecker
    {
        private static Dictionary<string, FileInfo> _fileInfoCache = new Dictionary<string, FileInfo>();

        public static bool CheckFileExists(string filePath)
    {
         FileInfo fileInfo;
        if (!_fileInfoCache.TryGetValue(filePath, out fileInfo))
        {
            fileInfo = new FileInfo(filePath);
            _fileInfoCache[filePath] = fileInfo;
        }
        Console.WriteLine(_fileInfoCache.Count);
        Console.WriteLine(fileInfo);
        return fileInfo.Exists;
    }
    }
}
